<?php

namespace app\admin\controller\auth;

use app\admin\dict\IconDict;
use app\admin\model\AuthRule;
use app\common\controller\BaseAdminController;
use app\common\library\addon\Addon;

class Rule extends BaseAdminController
{
    protected $authListt = [];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new AuthRule();
        $list = AuthRule::where('id','>',0)->order('weigh','desc')->order('id','asc')->select()->toArray();
        $list = list_to_tree($list,'id','pid','children',0);
        $this->authListt = $list;
    }
    public function index()
    {
//        return $this->selectpage();
        if($this->request->isAjax()){
            if($this->request->param('keyField')){
                return $this->selectpage();
            }
            return json(['rows'=>$this->authListt,'total'=>count($this->authListt)]);
//            return $this->success("获取成功",'',$this->authListt);
        }
        return $this->view->fetch();
    }
    public function edit($ids=null)
    {
        $row = $this->model->find($ids);
        if($this->request->isAjax()){
            $data = $this->request->param('row/a');
            $row->save($data);
            return $this->success("修改成功");
        }
        $this->view->assign('row',$row);
        return $this->view->fetch();

    }
    public function add()
    {
        if($this->request->isAjax()){
            $data = $this->request->param('row/a');
//            echo '<pre>';
//            print_r($data);
//            exit;
            $has_name = $this->model->where('name',$data['name'])
                ->find();
            if(!empty($has_name)){
                return $this->error("该权限已存在");
            }else{
                $this->model->save($data);
                AuthRule::getAllMenuStr(true);
                return $this->success("添加成功");
            }
        }
        return $this->view->fetch();
    }

    /**
     * 获取模块列表
     * @return mixed
     * @throws \Exception
     */
    public function get_modules()
    {
        $addons = get_addons_list();
        $modules = [];
        foreach ($addons as $k=>$v){
            $info = get_addons_info($v);
            if(!empty($info['title'])){
                $modules[] = [
                    'name'=>'addons/'.$v,
                    'title'=>$info['title'],
                ];
            }
        }
        $dir = app()->getRootPath().'app';
        $list = get_dir_path($dir,'dir',["common",'install']);
        foreach ($list as $k=>$v){
            $modules[] = [
                'name'=>$v,
                'title'=>ucfirst($v),
            ];
        }
        return $this->success("获取成功",'',$modules);
    }
    public function del($ids=null)
    {
        $lists = $this->model->whereIn('id',$ids)->with(['children'])->select()->toArray();
        foreach ($lists as $k=>$v){
            $this->delChild($v['children']);
        }
        $this->model->destroy($ids);
        AuthRule::getAllMenuStr(true);
        return $this->success("删除成功");
    }
    protected function delChild($list)
    {
        foreach ($list as $k=>$v){
            $childrens = $this->model->where('pid',$v['id'])->select()->toArray();
            $this->delChild($childrens);
            $this->model->destroy($v['id']);
        }
        return true;
    }

    /**
     * 扫描菜单
     * @return void
     */
    public function scan_menu()
    {
        set_time_limit(0);
        try {
            Addon::Menu();
        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
//        Addon::Menu();
        return $this->success("扫描成功");
    }
}